<template>
<div>
<template>

<!-- 添加学生隐藏端口 -->
<el-dialog title="添加学生" :visible.sync="dialogFormVisible">
  <el-form :model="student">
    <el-form-item label="学生姓名" label-width="120px">
      <el-input v-model="student.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="学生学号" label-width="120px">
      <el-input v-model="student.stuNo" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="学生生日" label-width="120px">
      <el-date-picker
        v-model="student.birthday"
        type="date"
        placeholder="选择生日">
      </el-date-picker>
    </el-form-item> 
    <el-form-item label="所属学院" label-width="120px">
      <el-select v-model="student.departmentId" placeholder="请选择">
        <el-option
          v-for="item in departments"
          :key="item.id"
          :label="item.departmentName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="所属班级" label-width="120px">
      <el-select v-model="student.classId" placeholder="请选择">
        <el-option
          v-for="item in classes"
          :key="item.id"
          :label="item.className"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureAddstudent()">添加</el-button>
      <el-button size="small"  type="primary" @click="cancelAddstudent()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 修改学生隐藏端口 -->
    <el-dialog title="修改学生" :visible.sync="updateDialogFormVisible">
  <el-form :model="ustudent">
    <el-form-item label="学生姓名" label-width="120px">
      <el-input v-model="ustudent.username" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="学生学号" label-width="120px">
      <el-input v-model="ustudent.stuNo" autocomplete="off"></el-input>
    </el-form-item> 
    <el-form-item label="学生生日" label-width="120px">
      <el-date-picker
        v-model="ustudent.birthday"
        type="date"
        placeholder="选择生日">
      </el-date-picker>
    </el-form-item> 
    <el-form-item label="所属学院" label-width="120px">
      <el-select v-model="ustudent.departmentId" placeholder="请选择">
        <el-option
          v-for="item in departments"
          :key="item.id"
          :label="item.departmentName"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="所属班级" label-width="120px">
      <el-select v-model="ustudent.classId" placeholder="请选择">
        <el-option
          v-for="item in classes"
          :key="item.id"
          :label="item.className"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label-width="200px">
      <el-button size="small"  type="primary" @click="ensureUpdatestudent()">修改</el-button>
      <el-button size="small"  type="primary" @click="cancelUpdatedstudent()">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>

  <!-- 页面主体 -->
   <el-row :gutter="0">

     <!-- 添加学生按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="addstudent()">增加学生</el-button>
    </el-col>

    <!-- 输入框 -->
    <el-col :span="6">
        <el-input
        v-model="studentDTO.username"
        class="w-50 m-2"
        size="small"
        placeholder="请输入姓名"
        @input="search()"
        />
    </el-col>

    <!-- 搜索按钮 -->
    <el-col :span="3">
      <el-button size="small"  type="primary" @click="search()">搜索</el-button>
    </el-col>
  </el-row>

  <!-- 表格 -->
  <el-table :data="students" stripe border height="400">
    <el-table-column prop="id" sortable=true label="ID"/>
    <el-table-column prop="username" sortable=true label="昵称"/>
    <el-table-column prop="stuNo" sortable=true label="学生学号" />
    <el-table-column prop="birthday" sortable=true label="学生生日" />
    <el-table-column prop="departmentName" sortable=true label="学生学院" />
    <el-table-column prop="className" sortable=true label="学生班级" />
        <el-table-column label="操作">
            <template #default="scope">
                <el-button size="small" @click="updatestudent(scope.row)">
                   修改
                </el-button>
                <el-button size="small" type="danger" @click="deletestudent(scope.row.id)">
                   删除
                </el-button>
            </template>
            </el-table-column>
  </el-table>

  <!-- 分页部分 -->
  <el-pagination
    small
    background
    layout="prev, pager, next"
    :page-size="studentDTO.capacity"
    :current-page.sync="studentDTO.index"
    @current-change="changeCurrent"
    :total="count"
    class="mt-4"
  />
</template>

</div>
</template>
<script>
export default {
  name: 'Home',
  data () {
    return {
      // 当前登录的用户角色
      role: '学生',
      // 表格映射对象
      students: [],
      // 修改的对象
      ustudent: {
        username: '',
        password: '',
        roleId: '',
        studentNo: ''
      },
      // 表格查询对象
      studentDTO: {
          index: 1,
          capacity: 10,
          username: ''
      },
      // 角色列表
      roles: [],
      // 添加学生弹出控制
      dialogFormVisible: false,
      // 修改学生弹出控制
      updateDialogFormVisible: false,
      // 添加学生实体
      student: {
          username: ' ',
          password: '',
          roleId: '',
          studentNo: ''
      },
      // 学生列表总数
      count: 0,
      departments: [],
      classes: [],
    }
  },
  // 页面初始化函数 
  created () {
    this.$http({
        url: "/student",
        params: {
            username: this.studentDTO.username
        }
    }).then(response => {
        console.log(response.data)
       this.students = response.data
    })
    this.$http({
        url: "/classes",
        params: {
            classesName:''
        }
    }).then(response => {
       this.classes = response.data
    })
    this.$http({
        url: "/department",
        params: {
            departmentName:''
        }
    }).then(response => {
       this.departments = response.data
    })
  },
  methods: {
    // 改变当前页
    changeCurrent() {
      findAll()
    },
    // 添加学生
     addstudent() {
       this.dialogFormVisible=true;
     },
     updatestudent(data) {
       this.ustudent=data
       this.updateDialogFormVisible=true
     },
     deletestudent(id) {
        this.$http.delete('/student/' + id).then(response => {
          this.$message({
          message: '删除学生成功',
          type: 'success'
          });
          this.findAll()
        })
     },
     ensureAddstudent() {
      this.$http.post("/student", this.student).then(response => {
        this.$message({
          message: '添加学生成功',
          type: 'success'
        });
        this.clearstudent()
        this.findAll()
      })
    },
    // 取消添加学生
    cancelAddstudent() {
      this.clearstudent()
    },
    ensureUpdatestudent() {
      this.$http.put('/student', this.ustudent).then(response => {
        this.$message({
          message: '修改学生成功',
          type: 'success'
        });   
        this.updateDialogFormVisible=false
        this.findAll()
      })
    },
    cancelUpdatedstudent() {
      this.updateDialogFormVisible=false
    },
    // 搜索对象，并映射
    search() {
      this.findAll()
    },
    // 查询所有，并映射
    findAll() {
      this.$http({
          url: "/student",
          params: {
              username: this.studentDTO.username
          }
      }).then(response => {
        this.$http.get('/student/count').then(response => {
          this.count = response.data
        })
        this.students = response.data
      })
    },
    // 清除学生，并关闭窗口
    clearstudent() {
        this.student.username=''
        this.student.password=''
        this.student.roleId=''
        this.student.studentNo=''
        this.dialogFormVisible=false
     }
  }
}
</script>
<style>

</style>